/*
 * Copyright 2009 Douglas Lockamy, dlockamy_at_gmail.com
 * All rights reserved. Distributed under the terms of the MIT License.
 */

if (!this.login) login = {};

login.version = '0.1';

login.buffer = '';

login.main = function()
{
    browserCenterX = document.body.clientHeight/2;
    
    loginWindowX = browserCenterX ;
    
    aFrame = new BFrame(loginWindowX,100,400,400);
    loginWindow = new BWindow(aFrame,"Login",null,null);

    var bframe = new BFrame(100,50,175,200);
    var bView = new BView(bframe,"bView",null,null);    
    bView.addElement("div","content");
    bView.SetHighColor("809090");
    loginWindow.addChild(bView);
        
    cmdRect = new BFrame(100,251,5,150);       
    cmdLine = new BInput(cmdRect,"commandline",null,null); 
    cmdLine.addElement("div","commandline");
    cmdLine.SetHighColor("809090");
    loginWindow.addChild(cmdLine );
    
    var logoFrame = new BFrame(0,280,80,80);  
    var logoView = new BImage(loginWindow.Bounds(),"logo","/System/icons/Softsurve.png");  
    logoView.SetHighColor("809090");
    var logoScroll = new BAnimation(logoView,"SHRINK",100,logoFrame);
    loginWindow.addChild(logoView);    
        
    benix.stdout('username: <br> ');
          
    loginWindow.Show();
    logoScroll.Play();

    while(benix.password)
        login.main(null);
};

login.input = function(keycode)
{
    if(keycode == '\\n')
    {  
        if(!benix.username)
        {
            benix.username = login.buffer; 
            login.buffer="";
            benix.stdout('Password:<br>');
            document.getElementById("commandline").value='> ';   
            return 0;
        }
        else
        {
            benix.password = login.buffer;
            login.buffer="";
            var homePath = '/home'+ '/'+benix.username;
            var remotePath = '/remote/0'
            if(vfs.mount('','userfs',remotePath,benix.username,benix.password,null) != -1 )
            {
                vfs.write(homePath,3,remotePath);
                //loginWindow.deleteWin();
                BRadicle.BRadicle();
                bash.main(); 
            }
            else
            {
                benix.username=0;
                benix.password=0;            
                benix.stdout('Bad username or password<br>');
                login.main();
            };
        }
    }
    else
        login.buffer = login.buffer + keycode;

    return 0;
};



